package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.domain.Sports;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.SportsService;
import com.itheima.util.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/*
    运动库表现层
 */
@RestController
@RequestMapping("/sports")
public class SportsController {

    //注入服务层接口
    @Reference
    private SportsService sportsService;

    //文件上传
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile){
        try {
            //读取Excel文件
            List<String[]> list = POIUtils.readExcel(excelFile);
            //对list进行判断
            if (list != null && list.size() > 0) {
                //list不为空 将list转为List<Food> 格式
                List<Sports> sportsList = new ArrayList<>();
                for (String[] strings : list) {
                    //将list中的每一个元素进行转化
                    String code = strings[0];
                    String campaignName = strings[1];
                    String theNumber = strings[2];
                    String sportTime = strings[3];
                    Sports sports = new Sports(code,campaignName,theNumber,sportTime);
                    sportsList.add(sports);
                }
                //调用添加方法
                sportsService.upload(sportsList);
            }
        } catch (IOException e) {
            e.printStackTrace();
            //上传失败,返回错误信息
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
        //上传成功,返回结果
        return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
    }


    //添加运动项
    @RequestMapping("/add")
    public Result add(@RequestBody Sports sports){
        try{
            //调用服务层接口
            sportsService.add(sports);
            //添加成功,返回结果
            return new Result(true,MessageConstant.ADD_SPORTS_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            //添加成功,返回结果
            return new Result(true,MessageConstant.ADD_SPORTS_FAIL);
        }
    }


    //分页查询运动项数据
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        //调用服务层接口
        PageResult pageResult = sportsService.findPage(queryPageBean);
        //返回结果
        return pageResult;
    }

    //编辑窗回显
    @RequestMapping("/findById")
    public Result findById(int id){
        try{
            //调用服务层接口
            Sports sports = sportsService.findById(id);
            //查询成功,返回结果
            return new Result(true,MessageConstant.QUERY_SPORTS_SUCCESS,sports);
        }catch (Exception e){
            e.printStackTrace();
            //查询失败,返回错误信息
            return new Result(false,MessageConstant.QUERY_SPORTS_FAIL);
        }
    }

    //编辑
    @RequestMapping("/edit")
    public Result edit(@RequestBody Sports sports){
        try{
            //调用服务层接口
            sportsService.edit(sports);
            //编辑成功,返回结果
            return new Result(true,MessageConstant.EDIT_SPORTS_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            //编辑失败,返回错误信息
            return new Result(false,MessageConstant.EDIT_SPORTS_FAIL);
        }
    }


    //删除运动项
    @RequestMapping("/delete")
    public Result delete(int id) {
        try {
            //调用服务层方法
            sportsService.delete(id);
            //删除成功,返回结果
            return new Result(true,MessageConstant.DELETE_SPORTS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //删除失败,返回错误信息
            return new Result(false, MessageConstant.DELETE_SPORTS_FAIL);
        }
    }


    //健康干预查询运动信息
    @RequestMapping("/findAll")
    public Result findAllSport(){
        try{
            //调用服务层接口
            List<Sports> list = sportsService.findAllSport();
            //查询成功,返回结果
            return new Result(true,MessageConstant.QUERY_SPORTS_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            //查询失败,返回错误信息
            return new Result(false,MessageConstant.QUERY_SPORTS_FAIL);
        }
    }
}
